jQuery(document).ready(function(){
	
	var form = jQuery("#maintenanceForm");  
	
	var name = jQuery("#maintenanceName");  
	
	var tel = jQuery("#maintenanceTel");  
	
	var email = jQuery("#maintenanceMail");
	
	//var address= jQuery("#maintenanceAddress");
	
	var postcode= jQuery("#maintenancePostcode");
	var dpost = jQuery("#dpost");
	
	var fax= jQuery("#maintenanceFax");
	
	var Ya = jQuery("#isgetmachine_Ya");
	
	var Na = jQuery("#isgetmachine_Na");
	var comment = jQuery("#maintenanceComment");
	var dcomment = jQuery("#dcomment");
	var address = jQuery("#maintenanceAddress");
	var daddress = jQuery("#daddress");
	var dname = jQuery("#dname");
	var dtel = jQuery("#dtel");
	var dmail = jQuery("#dmail");
	var  verify = jQuery("#validateCode");
	var dverify = jQuery("#dverify");
	var dfax = jQuery("#dfax");
	var province = jQuery("#province");
	var city = jQuery("#city");
	var pro1 = jQuery(".pro1");
	var city1 = jQuery(".city1");
	
	var flag = "false";
	var flag2 = "false";
	
	function validateArea() {
		if(province.val() == 0){
			prompt(province, pro1, "请选择省份");
			return false;
		}
		if(city.val() == 0){
			prompt(city, city1, "请选择地区");
			return false;
		}
		pro1.empty();
		return true;
	}
	function prompt(ele1,ele2,mess){
		ele2.css({"color":"red","display":"inline"});
	    ele2.html('*'+mess);
}
	function prompt2(ele1, ele2, mess) {
		var Y = jQuery(ele1).offset().top;
		var X = jQuery(ele1).offset().left;
		ele2.css({
			"left" : X + 68,
			"opacity" : "0.75",
			"position" : "absolute",
			"top" : Y-20,
			"color":"red"
		});
		ele2.html('<div class="formErrorContent">'+mess+'</div><div class="formErrorArrow"><div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div></div>');
	}
	function validateName() {  
		//var _name = name.val().toLocaleUpperCase();
		if (jQuery.trim(name.val()) == "") {
			name.addClass("error");
			prompt(name, dname, "姓名不能为空！");
			return false;
		//}else if(_name.indexOf("HTML")>-1||_name.indexOf("JAVASCRIPT")>-1||_name.indexOf("SQL")>-1){
	//		prompt(name, dname, "姓名不能出现特殊符号！");
	//		return false;
		} else{
			var len = name.val().length;
			if(len>20){
				name.addClass("error");  
				prompt(name,dname,"用户名在20个字符以内");
				return false;
			}
			var reg=/[\u4E00-\u9FA5]|[a-zA-Z]/g;
			for(var i=0;i<len;i++){
				if(!reg.test(name.val())){
				    name.addClass("error");  
					prompt(name,dname,"用户名含有非法字符");
					return false;
				}
			}
	        name.removeClass("error");  
	        dname.empty();
	        return true;  
		}
	}

	function validateTel() {
		if (jQuery.trim(tel.val()) == "") {
			tel.addClass("error");
			prompt(tel, dtel, "电话号码不能为空！");
			return false;
		}
		var patten = new RegExp(/^([+]{0,1}[0-9]{2,4}[ ]?[-]?)?(([0-9]){5,11})$/);
		if (tel.val() != "" && !patten.test(tel.val())) {
			tel.addClass("error");
			prompt(tel, dtel, "请查看电话号码格式！");
			return false;
		} else {
			tel.removeClass("error");
			dtel.empty();
			return true;
		}
		
	} 

	function validateEmail(){
		if(email.val()==""){
			email.removeClass("error");
			dmail.empty();
			return true;
		}
		var patten = new RegExp(/^[\w-]+([\w-]+)*@([\w-]+\.)+(com|cn)$/);
		if(!patten.test(email.val())){
			email.addClass("error");
			prompt(email,dmail,"邮箱格式不正确！");
			return false;
		}else{
			email.removeClass("error");
			dmail.empty();
			return true;
		}
	}
	
	function validateFax(){
		if(fax.val() == ""){
			fax.removeClass("error");
			dfax.empty();
			return true;
		}
		var patten = new RegExp(/^([+]{0,1}[0-9]{2,4}[ ]?[-]?)?(([0-9]){5,11})$/);
		if(!patten.test(fax.val())){
			fax.addClass("error");
			prompt(fax,dfax,"传真格式不正确！");
			return false;
		}else{
			fax.removeClass("error");
			dfax.empty();
			return true;
		}
	}
	
	function validatePostcode(){
		if(postcode.val() == ""){
			postcode.addClass("error");
			prompt(postcode,dpost,"邮编不能为空！");
			return false;
		}
		var patten = new RegExp(/^[0-9]{6}$/);
		if(!patten.test(postcode.val())){
			postcode.addClass("error");
			prompt(postcode,dpost,"邮编格式不正确！");
			return false;
		}else {
				//判断邮编和地区是否相符合

				var index = location.href.lastIndexOf("/");
				var basePath = location.href.substring(0,index);
				
				
				//flag= jQuery.ajax({url:basePath+"/getnat_code?city="+city.val()+"&natPost="+postcode.val(),async:false}).responseText;
				jQuery.ajax({
					url:+basePath+"/getnat_code?city="+city.val()+"&natPost="+postcode.val(),
					async:true,
					success: function(html){
						 flag = html;
						 if(flag == "nofind"){//不符合
								flag = "false";
							prompt(postcode,dpost,"对不起，该邮编不在服务范围内！");
							postcode.removeClass("error");
							}else{//符合
								dpost.empty();
							}
							return flag;
					 }
					});
				setTimeout(function(){},2000);
				if(flag == "nofind"){//不符合
					flag = "false";
				prompt(postcode,dpost,"对不起，该邮编不在服务范围内！");
				postcode.removeClass("error");
				}else{//符合
					dpost.empty();
				}
				return flag;
			}
		}
	


	function validateVerify(){
		
		if(verify.val()==""){
			prompt(verify,dverify,"验证码不能为空!");
		}else if(verify.val().length != 4){
			prompt(verify,dverify,"验证码位数为四位!");
			}else{
				var index = location.href.lastIndexOf("/");
				var basePath = location.href.substring(0,index);
				flag2= jQuery.ajax({url:basePath+"/getCode?validateCode="+verify.val(),async:false}).responseText;
				if(flag2 == "false"){
					jQuery("#Verify").attr("src","securityCodeImageAction?timestamp="+new Date().getTime());
					prompt(verify,dverify,"请输入正确验证码");
				}else{
					dverify.empty();
				}
			}
			return flag2;
		
	}
	
	function validateAddress(){
		//alert(address.val().length);
		if(address.val().length == 0){
			address.removeClass("comment2");
			prompt(address, daddress, "地址信息不能为空");
			return false;
		}
		if(address.val()=="请填写具体的地址信息"){
			prompt(address, daddress, "地址信息不能为空");
			address.removeClass("comment2");
			return false;
		}
	//	var _addres = address.val().toLocaleUpperCase();
		//if(_addres.indexOf("HTML")>-1||_addres.indexOf("JAVASCRIPT")>-1||_addres.indexOf("SQL")>-1){
	//		prompt(address, daddress, "不能出现特殊符号！");
	//		return false;
	//	}
		if(address.val().length>100){
			address.addClass("error");
			prompt(address, daddress, "不能超过100个字符！");
			return false;
		}else{
			address.removeClass("error");
			daddress.empty();
			return true;
		}
	}
	
	function validateComment(){
		//alert(comment.val().length);
		//var _comment = comment.val().toLocaleUpperCase();
		if(comment.val().length>100){
			comment.addClass("error");
			prompt2(comment, dcomment, "不能超过100个字符！");
			return false;
		//}else if(_comment.indexOf("HTML")>-1||_comment.indexOf("JAVASCRIPT")>-1||_comment.indexOf("SQL")>-1){
		//		prompt(comment, dcomment, "不能出现特殊符号！");
	//			return false;
		}else{
			if(address.val()=="（请填写您需要送修产品或附件的型号）"){
				address.attr("value",'');
			}
			comment.removeClass("error");
			dcomment.empty();
			return true;
		}
	}
	comment.click(function(){
		if(comment.val()=="（请填写您需要送修产品或附件的型号）"){
			comment.attr("value",'');
			comment.addClass("comment2");
		}
	});
	comment.blur(function(){
		if(comment.val()==""){
			comment.attr("value","（请填写您需要送修产品或附件的型号）");
			comment.removeClass("comment2");
		}
	});
	
	address.click(function(){
		if(address.val()=="请填写具体的地址信息"){
			address.attr("value",'');
			address.addClass("comment2");
		}
	});
	name.blur(validateName);  
	tel.blur(validateTel);  
	email.blur(validateEmail);
	fax.blur(validateFax);
	postcode.blur(validatePostcode);
	verify.blur(validateVerify);
	//address.blur(validateAddress);
	comment.blur(validateComment);
	verify.keyup(
			function(){
				if(verify.val().length>3){
					validateVerify();
				}
			});
	

	address.blur(function(){
		validateAddress();
		if(address.val()==""){
			address.attr("value",'请填写具体的地址信息');
		}
	});
	form.submit(function(){
	var value = jQuery("input[name='maintenance.isgetmachine'][type='radio']:checked").val();
	if(value == "Y"){
	    if(validateArea()&&validateAddress()&&validateName()&& validateTel()&&validateEmail()&&validateFax()&&validateComment()&&validatePostcode()=="true"&&validateVerify()=="true")  
	        return true;  
	    else  
	        return false;  
	}else{
		if(validateComment()&&validateVerify() =="true"){
	        return true;  
		}
	    else  {
	        return false;
	    }
	}
	});
	Ya.click(function(){
		jQuery("#isgetmachine_N").addClass("machine_onblur");
		jQuery("#isgetmachine_Y :input").removeProp("disabled");
		jQuery("#isgetmachine_Y").removeClass("machine_onblur");
		
		
	});
	Na.click(function(){
		jQuery("#isgetmachine_Y").addClass("machine_onblur");
		jQuery("#isgetmachine_Y :input").prop("disabled","true");
		jQuery("#isgetmachine_N").removeClass("machine_onblur");
	});

});
